<template>
  <div id='subtemp'>
      <div @click='substrict()' class="left div">-</div>
     <!--父组件页面重新渲染是，不会去执行子组件的created函数去从新赋值给count，所以页面只有在刷新会才会正确显示-->
      <!--如果想使用count只能在updated中重新赋值  -->
      <div class='center div' v-text="initcount"></div>
      <div @click="add()" class='right div'>+</div>
  </div>
</template>
<script>
export default {
    //props属性不能改写
    props:["initcount","goodsid"],
  data () {
    return {
        count:1,
        resobj:{goodsid:0,type:'add'},
    };
  },
  created(){
      this.count=this.initcount;
  },
  methods:{
      substrict(){
          this.count--;
          if(this.count<1){
              this.count=1;
            //   数量为1，就不能调用send方法，不能让localstorage执行减法操作
             return;
          }
          this.send('substrict');
      },
      add(){
          this.count++;
          this.send('add');
      },
      send(type){
          this.resobj.type=type;
        //   console.log('this'+this);
        //   console.log('this.goodsid'+this.goodsid);
          this.resobj.goodsid=this.goodsid;
          this.$emit("senddata",this.resobj);
      }

  }
}
</script>  
<style lang="css" scoped>
    #subtemp .div{
        border:1px solid rgba(0,0,0,.6);
        display: inline-block;
        width: 40px;
        height: 24px;
        line-height: 20px; 
        text-align: center;
    }
</style>